import {
    Dispatch
} from 'redux';

import ajax from './../../http';
import getUserId from './../../utils/userId';
import * as types from './../types/connection';

interface LimitPayload {
    limit?: number,
    isPush?: boolean,
    orderBy?: string,
    lastId: number | string
}

const FetchList = (
    url: string,
    payload: any,
    type: string
) => async (dispatch: Dispatch) => {
    const {
        lastId,
        isPush,
        orderBy,
        limit = 20
    } = payload;
    const userId = (
        getUserId()
    )
    const params = {
        limit: limit,
        lastId: lastId
    }
    try {
        const list = (
            await ajax.get(url, params)
        )
        const _data = {
            isPush: isPush,
            userId: userId,
            queryList: list,
            orderBy: orderBy
        }
        dispatch({
            type: type,
            data: _data
        })
        return list;
    }
    catch (e) {
        return Promise.reject(e)
    }
}

export default {
    //获取喜欢我的用户
    [types.getLikeMeUser](payload: LimitPayload) {
        return FetchList(
            `liked/me/users`,
            payload,
            types.getConnectionList
        )
    },
    //获取访问我的用户
    [types.getVisitedUser](payload: LimitPayload) {
        return FetchList(
            `visited/me/users`,
            payload,
            types.getConnectionList
        )
    },
    //获取我喜欢的用户
    [types.getILikeUser](payload: LimitPayload) {

        return FetchList(
            `i/liked/users`,
            payload,
            types.getConnectionList
        )
    },
    //获取互相喜欢对方的用户
    [types.getMatchedUser](payload: LimitPayload) {
        return FetchList(
            `mutual/likes`,
            payload,
            types.getConnectionList
        )
    }
}